﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rabbit.Logic;
using RabbitShop.Code;

namespace RabbitShop.UI
{
    public partial class OrderList : System.Web.UI.Page
    {
        protected List<OrderViewCustomer> list = new List<OrderViewCustomer>();
 
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = string.Empty;
            if (Request.Cookies[Constant.CookieName] != null)
            {
                uid = Request.Cookies[Constant.CookieName].Values[Constant.CookieUid];
                if (string.IsNullOrEmpty(uid)) Response.Redirect("~/UI/UserLogon.aspx");
            }
            else
            {
                Response.Redirect("~/UI/UserLogon.aspx");
            }


            Customer customer = RabbitUse.GetCustomerById(Db.Store, uid);
            CustomerOrderMapping customerOrderMapping = RabbitUse.GetCustomerOrderMappingById(Db.Store, customer.OrderMappingId);
            int count = customerOrderMapping.OrderIdList.Count;
            for (int i = 0; i < count;i++ )
            {
                string orderId = customerOrderMapping.OrderIdList[count - 1 - i];
                Order order = RabbitUse.GetOrderById(Db.Store, orderId);
                Shop shop = RabbitUse.GetShopByShopId(Db.Store, order.ShopId);
                OrderViewCustomer orderViewCustomer = RabbitPunch.GetOrderViewCustomer(Db.Store, order);
                list.Add(orderViewCustomer);
            }

            myRepeater.ItemDataBound += Repeater_ItemDataBound;
            //OrderList = RabbitUse.GetShopItemFullList(Db.Store, customer);
            myRepeater.DataSource = list;
            myRepeater.DataBind();

        }

        void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repeater = e.Item.FindControl("productRepeater") as Repeater;
                OrderViewCustomer data = (OrderViewCustomer)e.Item.DataItem;
                repeater.DataSource = data.ProductSnapWithQuantityList;
                repeater.DataBind();
            }
        }
    }
}